import com.mapper.WorkerMapper;
import com.pojo.Worker;
import com.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

public class WorkerTest {
    @Test
    public void findWorkerByIdTest(){
        SqlSession session = MyBatisUtils.getSession();
        WorkerMapper mapper = session.getMapper(WorkerMapper.class);

        Worker worker = mapper.SelectWorker(1);
        System.out.println(worker);
        session.close();
    }
    @Test
    public void insertWorkerTest(){
        SqlSession session = MyBatisUtils.getSession();
        Worker worker = new Worker();
        worker.setId(4);
        worker.setName("赵六");
        worker.setAge(36);
        worker.setSex("女");
        worker.setWorkerId(1004);
        WorkerMapper mapper = session.getMapper(WorkerMapper.class);
        int result = mapper.insertWorker(worker);
        if (result > 0){
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }
        System.out.println(worker.toString());
        session.commit();
        session.close();
    }
    @Test
    public void updateWorkerTest(){
        SqlSession session = MyBatisUtils.getSession();
        Worker worker = new Worker();
        worker.setId(4);
        worker.setName("李华");
        worker.setAge(28);
        WorkerMapper mapper = session.getMapper(WorkerMapper.class);
        int result = mapper.updatWorker(worker);
        if (result > 0){
            System.out.println("成功更新'+result+'条数据");
            }else {
            System.out.println("更新数据失败");
        }
        System.out.println(worker.toString());
        session.commit();
        session.close();
    }
    @Test
    public void deleteWorkerTest() {
        SqlSession session = MyBatisUtils.getSession();
        WorkerMapper mapper = session.getMapper(WorkerMapper.class);
        int result = mapper.deletWorker(4);
        if (result>0){
            System.out.println("成功删除"+result+"条数据");
        }else {
            System.out.println("删除数据失败");
        }
        session.commit();
        session.close();
    }
    @Test
    public void selectWorkerByIdAndNameTest(){
        SqlSession session = MyBatisUtils.getSession();
        WorkerMapper mapper = session.getMapper(WorkerMapper.class);
        Worker worker = mapper.selsctWorkerByIdAndName(3,"王五");
        System.out.println(worker);
        session.close();
    }
}
